//
//  ZFBBusinessHeaderTopView.m
//  01-ZFB
//
//  Created by My Mac on 2017/6/4.
//  Copyright © 2017年 MyMac. All rights reserved.
//

#import "ZFBBusinessHeaderTopView.h"
#import "ZFBBusinessHeaderTopFlowLayout.h"
#import "ZFBBusinessHeaderTopCell.h"
#import "ZFBBusinessHeaderTopList.h"
@interface ZFBBusinessHeaderTopView()<UICollectionViewDelegate,UICollectionViewDataSource>

@property (weak, nonatomic) UIPageControl* pageControl;

@end

@implementation ZFBBusinessHeaderTopView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setupUI];
    }
    return self;
}

- (void)awakeFromNib
{
    [super awakeFromNib];
    [self setupUI];
}

- (void)setupUI
{
    ZFBBusinessHeaderTopFlowLayout* layout = [[ZFBBusinessHeaderTopFlowLayout alloc] init];
    UICollectionView* collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
    collectionView.backgroundColor = [UIColor whiteColor];
    
    collectionView.dataSource = self;
    collectionView.delegate = self;
    [self addSubview:collectionView];
    
    [collectionView mas_makeConstraints:^(MASConstraintMaker* make) {
        make.top.left.right.offset(0);
        make.bottom.offset(-30);
        
    }];
    // 注册单元格
    [collectionView registerClass:[ZFBBusinessHeaderTopCell class] forCellWithReuseIdentifier:@"myItem"];
    
    //分页控件
    UIPageControl* pageControl = [[UIPageControl alloc] init];
    pageControl.pageIndicatorTintColor = [UIColor groupTableViewBackgroundColor];
    pageControl.currentPageIndicatorTintColor = [UIColor orangeColor];
    [self addSubview:pageControl];
    pageControl.numberOfPages = 2;
    
    [pageControl setEnabled:NO];
    
    [pageControl mas_makeConstraints:^(MASConstraintMaker* make) {
        make.centerX.equalTo(self);
        make.top.equalTo(collectionView.mas_bottom).offset(-8);
    }];
    _pageControl = pageControl;
}

- (NSInteger)collectionView:(UICollectionView*)collectionView numberOfItemsInSection:(NSInteger)section
{
    return _headerTopListData.count;
}

- (void)layoutSubviews {
    [super layoutSubviews];
    
}

- (UICollectionViewCell*)collectionView:(UICollectionView*)collectionView cellForItemAtIndexPath:(NSIndexPath*)indexPath
{
    ZFBBusinessHeaderTopCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"myItem" forIndexPath:indexPath];
    cell.headerTopList = _headerTopListData[indexPath.row];
    return cell;
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    ZFBBusinessHeaderTopCell *cell = (ZFBBusinessHeaderTopCell *) [collectionView cellForItemAtIndexPath:indexPath];
    ZFBBusinessHeaderTopList *headerTopList = cell.headerTopList;
    if ([self.delegate respondsToSelector:@selector(businessHeaderTopView:itemTitle:atIndex:)]) {
        [self.delegate businessHeaderTopView:self itemTitle:headerTopList.name atIndex:indexPath.item];
    }
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    CGFloat page = scrollView.contentOffset.x / self.bounds.size.width;
    NSInteger pageNo = page + 0.4999;
    _pageControl.currentPage = pageNo;
}

@end
